package com.aaa.controller;

import com.aaa.pojos.Result;
import com.aaa.util.AliyunOSSOperator;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.MultipartFilter;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class ImgUploadController {
    @Resource
    private AliyunOSSOperator ossOperator;

    /**
     * 接收文件数据：
     *  上传文件使用的是表单格式，因为使用@RequestParam接收
     *  如果方法形参名 与 表单参数名相同，则可以省略@RequestParam不写
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        if (!file.isEmpty()) {
            String url = ossOperator.upload(file.getBytes(),file.getOriginalFilename());
            return Result.success(url);
        }
        return Result.error("上传的文件不能为空！");
    }
}

//    @PostMapping("/upload")
//    public Result upload(MultipartFile file) throws IOException {
//        if(!file.isEmpty()){
//            String filename = file.getOriginalFilename();
//            String substring = filename.substring(filename.lastIndexOf("."));
//            filename= UUID.randomUUID()+substring;
//            file.transferTo(new File("D:\\"+ filename));
//        }
//        return Result.success();
//    }
//}
